package com.nus.servlets;

import com.nus.beans.Leave;
import com.nus.beans.LeaveStatus;
import com.nus.beans.User;
import com.nus.beans.UserType;
import com.nus.database.LeavesDBManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 9/23/12
 * Time: 1:00 PM
 * To change this template use File | Settings | File Templates.
 */
public class PendingLeavesServlet extends AbstractServlet{
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        User user = (User) request.getSession().getAttribute("user");
        UserType userType = user.getUserType();
        LeaveStatus leaveStatus = null;
        List<Leave> leaves;

        String targetPage = null;

        if(userType.equals(UserType.PM)){
            leaveStatus = LeaveStatus.APPLIED;
            targetPage = "/WEB-INF/leaves_to_verify.jsp";
        } else if(userType.equals(UserType.HR_MGR) || userType.equals(UserType.HR_ASST)){
            leaveStatus = LeaveStatus.VERIFIED;
            targetPage = "/WEB-INF/leaves_to_approve.jsp";
        }

        Connection connection = getUserDatabaseConnection(request);
        LeavesDBManager leavesDBManager = new LeavesDBManager(connection);
        leaves = leavesDBManager.getPendingLeaves(leaveStatus);

        leavesDBManager.close();

        request.setAttribute("leavesList", leaves);

        RequestDispatcher requestDispatcher = request.getRequestDispatcher(targetPage);
        requestDispatcher.forward(request, response);
    }
}
